package test1;

import java.io.*;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Test {
    //菜单
    public static void menu() {
        System.out.println("==================================");
        System.out.println("               菜单                ");
        System.out.println("     1.办理新卡                      ");
        System.out.println("     2.取款                        ");
        System.out.println("     3.存款                        ");
        System.out.println("     4.查余额                      ");
        System.out.println("     5.查密码                      ");
        System.out.println("     6.修改密码                    ");
        System.out.println("     7.显示完整信息                 ");
        System.out.println("     8.转账                        ");
        System.out.println("     9.计算利息                     ");
        System.out.println("     0.退出                        ");
        System.out.println("==================================");

    }

    //办理新卡
    public static BankCardnew newCard() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请先办理一张新卡");
        System.out.println("请选择你开户行");
        System.out.println("--------------------");
        System.out.println("    1. 工商银行");
        System.out.println("    2. 农业银行");
        System.out.println("    3. 交通银行");
        System.out.println("    0. 退出");
        System.out.println("--------------------");
        while(true) {
            int input = sc.nextInt();
            if(input<0 || input>3) {
                System.out.println("输入有误,请重新输入");
                continue;
            }
            System.out.println("请输入你的身份证号: ");
            String uid = sc.next();
            System.out.println("请输入你的姓名: ");
            String name = sc.next();
            switch (input) {
                case 1:
                    BankCardnew ICBCcard = new FinancialCard(uid, name);//工行卡
                    System.out.println("工行卡办理成功!");
                    return ICBCcard;
                case 2:
                    BankCardnew ABCBcard = new ABCBankCard(uid, name);//农行卡
                    System.out.println("农行卡办理成功!");
                    return ABCBcard;
                case 3:
                    BankCardnew BCMBcard = new BCMBankCard(uid, name);//交行卡
                    System.out.println("交行卡办理成功!");
                    return BCMBcard;
                case 0:
                    return null;
            }
        }
    }

    /**
     * 字节流写文件
     * @param
     */
    public static void writeFile(  List<BankCardnew> cardList) throws IOException {
        File file = new File("D://text//test02.txt");
        //设为true,将后续写入的内容追加在后面
        OutputStream outputStream = new FileOutputStream(file,true);
        PrintStream printStream = new PrintStream(outputStream);
        for (BankCardnew card:cardList) {
            printStream.println(card.toString());
            printStream.flush(); //清除缓冲区
        }
        outputStream.close();
    }

    /**
     * 字节流读文件
     * @param
     * @throws IOException
     */
    public static String readFile() throws IOException {
        InputStream inputStream = new FileInputStream("D://text//test02.txt");
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);

        byte []b = new byte[1024]; // 创建一个字节数组,作为read方法的参数
        int len = bufferedInputStream.read(b);
        String s = new String(b ,0,len,"UTF-8");
        inputStream.close();
        return s;
    }

    /**
     * 字符流写文件
     * @param
     */
    public static void writeFile1(  List<BankCardnew> cardList) throws IOException {
        File file = new File("D://text//test02.txt");
        FileWriter fileWriter= new FileWriter(file,true);
        PrintWriter printWriter = new PrintWriter(fileWriter);
        for (BankCardnew card:cardList) {
            printWriter.println(card.toString());
            printWriter.flush(); //清除缓冲区
        }
        for (BankCardnew card: cardList) {
            fileWriter.write(card.toString()+"\n");
        }
        fileWriter.close();
    }

    /**
     * 字符流读文件
     * @param
     */
    public static String readFile1() throws IOException {
        File file = new File("D://text//test02.txt");
        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String s;
        StringBuilder str = new StringBuilder();
        char []buffer=new char[1024];
        while((s = bufferedReader.readLine())!=null){
            str.append(s+"\n");
        }
        return str.toString();
    }
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        int input=0;
        int money = 0;
        boolean flag = false;
        System.out.println("已存在银行卡如下: ");
        System.out.println(readFile1());
        List<BankCardnew> cardList = new ArrayList<>();
        BankCardnew bank = null;
        do {
            if( cardList.size() == 0) {  //当没有 银行卡时,先办理一张银行卡
                input = 1;
            } else {
                if(  !flag ) {
                    System.out.println("请输入登录密码:");
                    String pwd = sc.next();
                    if( bank.checkPwdRight(pwd)) {
                        flag = true;
                    } else {
                        System.out.println("密码错误请重新输入");
                        continue;
                    }
                }
                 System.out.println( "你好, "+ bank.getName() + ": ");
                 menu();
                 System.out.println("请选择你要进行的操作>:");
                 input = sc.nextInt();
            }

            switch (input) {
                case 1:                 //创建新卡
                    bank = newCard();
                    cardList.add(bank);
                    System.out.println("新银行卡信息: " + bank.toString());
                    break;
                case 2:                  //取款
                    System.out.println("请输入你要取款的金额:");
                    money = sc.nextInt();
                    try {
                        if(bank.drawMoney(money)) {
                            System.out.println("取款成功, 余额是: "+ bank.getBalance());
                        } else{
                            System.out.println("输入有误或余额不足, 取款失败");
                        }
                    } catch (MoneyException e) {
                        System.out.println(e.getMessage());
                        e.printStackTrace();
                    }
                    break;
                case 3:                 //存款
                    System.out.println("请输入你要存款的金额:");
                    money = sc.nextInt();
                    try {
                        if( bank.deposit(money) ) {
                            System.out.println("cun款成功, 余额是: "+ bank.getBalance());
                        } else {
                            System.out.println("输入有误, 存款失败");
                        }
                    } catch (MoneyException e) {
                        System.out.println(e.getMessage());
                        e.printStackTrace();
                    }
                    System.out.println("余额是: "+ bank.getBalance());
                    break;
                case 4:                 //查询余额
                    double balance = bank.getBalance();
                    System.out.println("余额为: "+ balance);
                    break;
                case 5:                 //查询密码
                    System.out.println("密码为: "+ bank.getPassword());
                    break;
                case 6:                 //修改密码
                    System.out.println("请输入旧密码");
                    String oldpwd = sc.next();
                    System.out.println("请输入要修改的新密码>: ");
                    String newpwd = sc.next();
                    if(bank.changePwd(oldpwd,newpwd)) {
                        System.out.println("修改成功!");
                        System.out.println("新密码是: " + bank.getPassword());
                    } else {
                        System.out.println("修改失败,密码位数不正确或旧密码错误");
                    }

                    break;
                case 7:                 //显示完成信息
                    System.out.println("操作后的完整信息是>: ");
                    System.out.println(bank.toString());
                    break;
                case 8:                 //转账
                    System.out.println("请输入你要转账的银行卡号");
                    long toCardId = sc.nextLong();
                    BankCardnew tobank = null;
                    int i=0;
                    for( i=0;i<cardList.size();i++) {
                        if( cardList.get(i).cardNo == toCardId) {
                            tobank = cardList.get(i);
                            break;
                        }
                    }
                    if(i>=cardList.size()) {
                        System.out.println("要转账的用户不存在!");
                        break;
                    }
                    System.out.println("请输入转账金额>: ");
                    double tMoney = sc.nextDouble();
                    if( bank.transferMoney(tobank,tMoney) ) {
                        System.out.println("转账成功!");
                    } else {
                        System.out.println("转账失败!");
                    }
                    break;
                case 9:                 //计算利息
                    try {
                        countInterest(bank);
                    } catch (Exception e) {
                        e.printStackTrace();
                        System.out.println("输入的日期格式不正确,请重新输入");
                        continue;
                    }
                    break;
                case 0:
                    System.out.println("退出系统");
                    break;
                default:
                    System.out.println("输入错误,请重新输入!");
                    break;
            }
        } while (input != 0);
        //将list中的银行卡写入文件中
        writeFile1(cardList);
        //readFile();
    }

    /**
     * 计算利息
     */
    private static void countInterest(BankCardnew bank)  throws Exception{
        Scanner sc= new Scanner(System.in);
        System.out.println("请输入你计算利息的金额 : ");
        double money = sc.nextDouble();
        //转换字符串为日期格式
        CheckDateS checkDateS = new CheckDateS();
        System.out.println("请输入你存款的开始日期");
        String begin = sc.next();
        System.out.println("请输入你存款的结束日期");
        String end = sc.next();
        LocalDate beginDate = checkDateS.changeDateFormat(begin);
        LocalDate endDate = checkDateS.changeDateFormat(end);
        //使用向下转型
        FinancialCard financialCard = (FinancialCard) bank;
        String amountInterest = financialCard.countInterest(money, financialCard.interest, beginDate, endDate);
        System.out.println("当前时间段内可产生的利息为: "+ amountInterest + "元");
    }

}
